﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;
using System.Threading;

namespace CheckingInvoiceResultsConsole
{
    class Program
    {
        static string[] A,B,C ;
        
        static void Main(string[] args)
        {
            Thread th = new System.Threading.Thread(GetInvoiceData);
            th.SetApartmentState(System.Threading.ApartmentState.STA);
            th.Start();
            th.Join();

            string input = "";
            //Console.Write("請輸入號碼：");
            //input = Console.ReadLine();
            do
            {
                Console.Write("請輸入號碼：");
                input = Console.ReadLine();
                
                if (input.Length == 0) break;

                //TODO: 判斷
                bool bg = false;
                for (int i = 0; i < A.Length; i++)
                {
                    if (input.Substring(input.Length - 3) == A[i].Substring(A[i].Length - 3))
                    {
                        bg = true;
                        Console.WriteLine("中特獎了!!  " + A[i]);
                        break;
                    }
                }
                if (bg) continue;
                for (int i = 0; i < B.Length; i++)
                {
                    if (input.Substring(input.Length - 3) == B[i].Substring(B[i].Length - 3))
                    {
                        bg = true;
                        Console.WriteLine("中頭獎了!!  " + B[i]);
                        break;
                    }
                }

                if (bg) continue;
                for (int i = 0; i < C.Length; i++)
                {
                    if (input.Substring(input.Length - 3) == C[i].Substring(C[i].Length - 3))
                    {
                        bg = true;
                        Console.WriteLine("中增開獎了!!  " + C[i]);
                        break;
                    }
                }
                if (bg==false) Console.WriteLine("沒中...");

            } while (input.Length>=3);
           

        }

        public static void GetInvoiceData()
        {
            string strURL = "http://invoice.etax.nat.gov.tw/etaxinfo_1.htm";

            Type tp = Type.GetTypeFromProgID("InternetExplorer.Application");
            object ie = Activator.CreateInstance(tp);

            tp.InvokeMember("Navigate", BindingFlags.InvokeMethod, null, ie, new object[] { strURL });
            // tp.InvokeMember("Visible", BindingFlags.SetProperty, null, ie, new object[] { true });

            int intWait = 0;
            bool blnBusy = false;
            int intState = 0;


            while (true)
            {
                Thread.Sleep(500);
                blnBusy = (bool)tp.InvokeMember("Busy", BindingFlags.GetProperty, null, ie, null);
                intState = (int)tp.InvokeMember("ReadyState", BindingFlags.GetProperty, null, ie, null);
                if (!blnBusy && (intState == 4)) break;
                if (intWait >= 10)
                {
                    return;
                }
                Thread.Sleep(1000);
                intWait++;
            }

            object doc = tp.InvokeMember("document", BindingFlags.GetProperty, null, ie, null);

            string strState = "";
            intWait = 0;

            while (true)
            {
                Thread.Sleep(500);
                strState = (string)tp.InvokeMember("readyState",
                                                    BindingFlags.GetProperty,
                                                    null,
                                                    doc,
                                                    null);
                if (strState.Equals("complete")) break;
                if (intWait >= 10)
                {
                    return;
                }
                Thread.Sleep(1000);
                intWait++;
            }

            object objAll = tp.InvokeMember("body", BindingFlags.GetProperty, null, doc, null);

            //tp.InvokeMember("body", BindingFlags.GetProperty, null, doc, null);
            string context = (string)tp.InvokeMember("innerHTML", BindingFlags.GetProperty, null, objAll, null);

            WebBrowser wb = new WebBrowser();
            wb.Navigate("");
            wb.ScriptErrorsSuppressed = true;
            //wb.Document.Write("<script type='text/javascript'>window.onerror = function(){};</script>" + context);
            wb.Document.Write(context);
            HtmlElementCollection tb = wb.Document.GetElementsByTagName("TABLE");
            tb = wb.Document.GetElementsByTagName("TABLE");
            HtmlElementCollection tr = tb[0].GetElementsByTagName("TR");

            //TODO:1 3 10
            //Console.WriteLine(tr[1].InnerText);
            //Console.WriteLine(tr[3].InnerText);
            //Console.WriteLine(tr[10].InnerText);
            Console.WriteLine("*********************************************************");
            HtmlElementCollection tdA = tr[1].GetElementsByTagName("TD");
            HtmlElementCollection tdB = tr[3].GetElementsByTagName("TD");
            HtmlElementCollection tdC = tr[10].GetElementsByTagName("TD");

            //Console.WriteLine(tdC[0].InnerText);

            A = tdA[0].InnerText.Split('、');
            B = tdB[0].InnerText.Split('、');
            C = tdC[0].InnerText.Split('、');
            Console.WriteLine("特獎");
            for (int i = 0; i < A.Length; i++)
            {
                Console.WriteLine(A[i]);
            }
            Console.WriteLine("頭獎");
            for (int i = 0; i < B.Length; i++)
            {
                Console.WriteLine(B[i]);
            }
            Console.WriteLine("增開獎");
            for (int i = 0; i < C.Length; i++)
            {
                Console.WriteLine(C[i]);
            }
            Console.WriteLine("*********************************************************");

            //釋放資源
            wb.Dispose();
            Marshal.ReleaseComObject(ie);
            ie = null;
        }
    }
}
